package org.example.student_dormitory.controller;

import net.sf.json.JSONObject;
import org.example.student_dormitory.dto.Result;
import org.example.student_dormitory.service.AdminManagerService;
import org.example.student_dormitory.service.impl.AdminManagerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/adminManager")
public class AdminManagerServlet extends HttpServlet {

    private AdminManagerService adminManagerService = new AdminManagerServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置请求响应编码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json");

        // 获取前端请求数据
        String method = req.getParameter("method");

        switch (method){
            case "findAll"://数据首次加载
                findAll(req,resp);
                break;
            case "add"://添加管理员
                addAdmin(req,resp);
                break;
            case "checkUsername" ://用户名校验
                checkUsername(req,resp);
                break;
            case "delete" ://删除
                deleteAdmin(req,resp);
                break;
            case "edit" ://修改
                editAdmin(req,resp);
                break;
            case "paging" ://分页
                paging(req,resp);
                break;
            case "deleteList":
                deleteAdminList(req,resp);
                break;
            default:
                break;
        }
    }

    private void deleteAdminList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String[] idArrs = req.getParameterValues("id_arr[]");
        Result result = adminManagerService.deleteList(idArrs);

        String json_str = JSONObject.fromObject(result).toString();
        resp.getWriter().write(json_str);
    }

    private void paging(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //接收数据
//        System.out.println("已经调用了paging");

        String page = req.getParameter("page");
        String pageSize = req.getParameter("pageSize");
        String key = req.getParameter("key");
        String value = req.getParameter("value");

//        System.out.println("准备进入Service");
        Result result = adminManagerService.loadtable(page,pageSize,key,value);

        String json_str = JSONObject.fromObject(result).toString();
        resp.getWriter().write(json_str);
    }

    private void editAdmin(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String name = req.getParameter("name");
        String gender = req.getParameter("gender");
        String phone = req.getParameter("phone");
        String role = req.getParameter("role");

//        System.out.println("准备进入Service");
        Result result = adminManagerService.updateAdmin(id,username,password,name,gender,phone,role);
//        System.out.println("从Service中返回");
        String json_str = JSONObject.fromObject(result).toString();
        resp.getWriter().write(json_str);
    }

    private void deleteAdmin(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取id
        String id = req.getParameter("id");
        Result result = adminManagerService.deleteById(id);

        String json_str = JSONObject.fromObject(result).toString();
        resp.getWriter().write(json_str);
    }

    private void checkUsername(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String username = req.getParameter("username");
        Result result = adminManagerService.checkUsername(username);

        String json_str = JSONObject.fromObject(result).toString();
        resp.getWriter().write(json_str);
    }

    private void addAdmin(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //接受前端提交的数据
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String name = req.getParameter("name");
        String gender = req.getParameter("gender");
        String phone = req.getParameter("phone");
        String role = req.getParameter("role");
        Result result = adminManagerService.addAdmin(username,password,name,gender,phone,role);

//        System.out.println("Service调用完毕");
        // 将java对象转json字符串
        String json_str = JSONObject.fromObject(result).toString();
        // 将json字符串响应给前端
        resp.getWriter().write(json_str);
    }

    private void findAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String pageSize = req.getParameter("pageSize");
        Result result = adminManagerService.loadtable(page, pageSize);

        // 将java对象转json字符串
        String json_str = JSONObject.fromObject(result).toString();
        // 将json字符串响应给前端
        resp.getWriter().write(json_str);
    }
}
